package benny.ApkposStore.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import benny.ApkposStore.db.DBHelper;
import benny.ApkposStore.db.OrderTableNew;
import benny.ApkposStore.model.DishNew;
import benny.ApkposStore.model.OrderNew;

public class ApkDbUntils extends Activity {

	// 查询获得当前菜单主表信息
	public static OrderNew queryorder(SQLiteDatabase db, Context cx,
			String orderid) {
		// TODO Auto-generated method stub

		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,0);
		}
		OrderNew data = new OrderNew();
		try {
			OrderTableNew ordertablenew = new OrderTableNew(cx);
			String selection = "orderid=?";
			String selectionArgs[] = { orderid };
			Cursor curSor = ordertablenew.getCurSor(selection, selectionArgs);

			while (curSor.moveToNext()) {
				data = ordertablenew.CurSorToGasBean(curSor);

			}
			curSor.close();
			// System.out.println(data);

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_new", "checkbill  error:" + e.getMessage());
		}

		return data;
	}

	// 设置打折信息
	public static void setupbill(SQLiteDatabase db, Context cx, String orderid,
			String dic) {
		// TODO Auto-generated method stub

		try {
			if (db == null || !db.isOpen()) {
				db = DBHelper.getDatabase(cx,1);
			}
			db.execSQL("update  apkpos_orders_new set dic_rate=" + dic
					+ " where orderid='" + orderid + "'  ");
			db.execSQL("update  apkpos_orders_dishs set dic_rate=" + dic
					+ " where orderid='" + orderid + "'  ");

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_new", "setupbill  error:" + e.getMessage());
		}
	}

	// 查单
	public static ArrayList<OrderNew> checkbill(SQLiteDatabase db, Context cx) {
		// TODO Auto-generated method stub
		ArrayList<OrderNew> data = null;
		try {
			if (db == null || !db.isOpen()) {
				db = DBHelper.getDatabase(cx,0);
			}
			OrderTableNew ordertablenew = new OrderTableNew(cx);
			String selection = "room_state=?";
			String selectionArgs[] = { "5" };
			Cursor curSor = ordertablenew.getCurSor(selection, selectionArgs);
			data = new ArrayList<OrderNew>();
			while (curSor.moveToNext()) {
				data.add(ordertablenew.CurSorToGasBean(curSor));

			}
			curSor.close();

			System.out.println("checkbill：" + data);

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_new", "checkbill  error:" + e.getMessage());
		}

		return data;
	}

	// 挂单
	public static void holdbill(SQLiteDatabase db, Context cx, String orderid) {
		// TODO Auto-generated method stub
		try {
			if (db == null || !db.isOpen()) {
				db = DBHelper.getDatabase(cx,1);
			}
			db.execSQL("update  apkpos_orders_new set room_state=5 where orderid='"
					+ orderid + "'  ");

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_new", "holdbill  error:" + e.getMessage());
		}
	}

	// 全单取消
	public static void deletedishall(SQLiteDatabase db, Context cx,
			String orderid) {
		// TODO Auto-generated method stub
		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,1);
		}
		try {
			db.execSQL("delete from apkpos_orders_dishs where orderid='"
					+ orderid + "' and order_state=0 ");
			db.execSQL("update apkpos_orders_dishs set dish_num=0 where orderid='"
					+ orderid + "' and order_state=1 ");
		} catch (SQLiteException e) {
			Log.e("apkpos_orders_dishs", "deleteall apkpos_orders_dishs error:"
					+ e.getMessage());
		}
	}
	

		
	// 更新菜品表信息
	public static List<DishNew> updatedishlist(SQLiteDatabase db, Context cx,String orderid) {

		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,0);
		}
		String args[] = { orderid };
		Cursor cursor;
		cursor = db.rawQuery(
				"select * from apkpos_orders_dishs where orderid=?", args);
		System.out.println(cursor.getCount());
		ArrayList<DishNew> data = new ArrayList<DishNew>();
		while (cursor.moveToNext()) {
			DishNew dishnew = new DishNew();
			dishnew.setId(cursor.getPosition()+1);
			dishnew.setOrderid(cursor.getString(cursor
					.getColumnIndex("orderid")));
			dishnew.setDnam_var(cursor.getString(cursor
					.getColumnIndex("dnam_var")));
			dishnew.setDish_unit(cursor.getString(cursor
					.getColumnIndex("dish_unit")));
			dishnew.setDish_time(cursor.getString(cursor
					.getColumnIndex("dish_time")));
			dishnew.setFree_state(cursor.getInt(cursor
					.getColumnIndex("free_state")));
			dishnew.setOrder_state(cursor.getInt(cursor
					.getColumnIndex("order_state")));
			dishnew.setOrder_mode(cursor.getString(cursor
					.getColumnIndex("order_mode")));
			dishnew.setDcy_name(cursor.getString(cursor
					.getColumnIndex("dcy_name")));
			dishnew.setCzy_name(cursor.getString(cursor
					.getColumnIndex("czy_name")));
			dishnew.setSvc_state(cursor.getInt(cursor
					.getColumnIndex("svc_state")));
			dishnew.setCook_content(cursor.getString(cursor
					.getColumnIndex("cook_content")));
			dishnew.setPdet_var(cursor.getString(cursor
					.getColumnIndex("pdet_var")));
			dishnew.setLbxh(cursor.getInt(cursor.getColumnIndex("lbxh")));
			dishnew.setLbname(cursor.getString(cursor.getColumnIndex("lbname")));

			dishnew.setDish_num(cursor.getInt(cursor
					.getColumnIndex("dish_num")));
			dishnew.setDish_price(cursor.getInt(cursor
					.getColumnIndex("dish_price")));
			dishnew.setDic_rate(cursor.getInt(cursor
					.getColumnIndex("dic_rate")));
			dishnew.setCook_extra(cursor.getInt(cursor
					.getColumnIndex("cook_extra")));
			dishnew.setOriginal_price(cursor.getInt(cursor
					.getColumnIndex("original_price")));

			dishnew.setDish_id(cursor.getInt(cursor.getColumnIndex("dish_id")));
			dishnew.setUnit_id(cursor.getInt(cursor.getColumnIndex("unit_id")));
			dishnew.setDnbr_var(cursor.getInt(cursor.getColumnIndex("dnbr_var")));

			data.add(dishnew);


		}
		cursor.close();
		// System.out.println(data);
		return data;

	}
	
	//删除某道菜
	public static void deletedish(SQLiteDatabase db, Context cx,String orderid, String dish_id) {
		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,1);
		}
		try {
			db.execSQL("delete from apkpos_orders_zuoflist where orderid='"
					+ orderid + "' and dish_id=" + dish_id + " ");
			db.execSQL("delete from apkpos_orders_dishs where orderid='"
					+ orderid + "' and dish_id=" + dish_id + " ");
		} catch (SQLiteException e) {
			Log.e("apkpos_orders_dishs", "delete apkpos_orders_dishs error:"
					+ e.getMessage());
		}
	}
	
	//修改菜品数量
	public static void changedishnum(SQLiteDatabase db, Context cx,String flag, int num, String orderid,
			String dish_id) {
		try {
			if (db == null || !db.isOpen()) {
				db = DBHelper.getDatabase(cx,1);
			}
			if (flag.equals("add")) {
				db.execSQL("update  apkpos_orders_dishs set dish_num=dish_num+"
						+ num + " where orderid='" + orderid + "' and dish_id="
						+ dish_id + " ");
			} else {
				db.execSQL("update  apkpos_orders_dishs set dish_num=dish_num-"
						+ num + " where orderid='" + orderid + "' and dish_id="
						+ dish_id + " ");
			}

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_dishs", "changenum apkpos_orders_dishs error:"
					+ e.getMessage());
		}
	}
	
	//设置某道菜的折扣率
	public static void setupdishrate(SQLiteDatabase db, Context cx,String orderid, String dishid, String dic) {
		// TODO Auto-generated method stub
		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,1);
		}
		try {
			db.execSQL("update  apkpos_orders_dishs set dic_rate=" + dic
					+ " where orderid='" + orderid + "' and dish_id='" + dishid
					+ "'  ");

		} catch (SQLiteException e) {
			Log.e("apkpos_orders_dishs",
					"setupdishrate  error:" + e.getMessage());
			e.printStackTrace();
		}
	}
	

}
